# 13/100 数组-最大子数组和
# 同dp_17_18
# leetcode第53题: https://leetcode.cn/problems/maximum-subarray/description/?envType=study-plan-v2&envId=top-100-liked
# Date: 2024/11/22
from leetcode import test


def maxSubArray(nums: list[int]) -> int:
    n = len(nums)
    if n < 2:
        return nums[0]

    dp = [0] * n
    dp[0] = nums[0]
    for i in range(1, n):
        dp[i] = max(dp[i - 1] + nums[i], nums[i])
    return max(dp)


if __name__ == '__main__':
    inp = [{"nums": [-2, 1, -3, 4, -1, 2, 1, -5, 4]}, {"nums": [5, 4, -1, 7, 8]}, ]
    out = [6, 23]
    test.test_function(maxSubArray, inp, out)
